var deviceone = require("deviceone");
var app = deviceone.sm("do_App");
var global = deviceone.sm("do_Global");
var cacher = deviceone.sm("do_DataCache");
var nf = deviceone.sm("do_Notification");

/** 用户id **/
var mapKey_user_id = "userId";
/** 用户token缓存key */
var mapKey_user_login_token = "userLoginToken";
/** 用户头像 **/
var mapKey_user_logo = "userLogo";
/** 用户昵称 **/
var mapKey_user_nick = "userNick";
/** 是否已身份认证 **/
var mapKey_hasIdentityAuth = "hasIdentityAuth";
/** 是否已身份认证 **/
var mapKey_hasCommunityAuth = "mapKeyHasCommunityAuth";

var auth_key = "qquantech2016";
var auth_value = "tech@xfquanquan.com";

module.exports.userId = function(){
	if(arguments.length==0){
		if(cacher.hasData(mapKey_user_id)){
			var value = cacher.loadData(mapKey_user_id);
			if(!value || value==null || value==="undefined"){
				return "";
			}
			return value;
		}else{
			return "";
		}
    }
    if(arguments[0]===""){
    	cacher.removeData(mapKey_user_id);
        return;
    }
    cacher.saveData(mapKey_user_id,""+arguments[0]);
};
module.exports.saveUserPara = function() {
	if (arguments.length == 3) {
		var userId = arguments[0];
		var field = arguments[1];
		var value = encodeURI(arguments[2]);
		cacher.saveData(userId+"_"+field, "" + value);
	}
	else {
		return "";		
	}
	
	
};

module.exports.loadUserPara = function() {
	if (arguments.length == 2) {
		var userId = arguments[0];
		var field = arguments[1];
		if (cacher.hasData(userId+"_"+field)) {
			var value = decodeURI(cacher.loadData(userId+"_"+field));
			if (!value || value == null || value === "undefined") {
				return "";
			}
			return value;
		}
	}
	else {
		return "";		
	}
};

module.exports.loginToken = function(){
	if(arguments.length==0){
		if(cacher.hasData(mapKey_user_login_token)){
			var value = cacher.loadData(mapKey_user_login_token);
			if(!value || value==null || value==="undefined"){
				return "";
			}
			return value;
		}else{
			return "";
		}
    }
    if(arguments[0]===""){
    	cacher.removeData(mapKey_user_login_token);
        return;
    }
    cacher.saveData(mapKey_user_login_token,""+arguments[0]);
};
module.exports.userLogo = function(){
	if(arguments.length==0){
		if(cacher.hasData(mapKey_user_logo)){
			var value = cacher.loadData(mapKey_user_logo);
			if(!value || value==null || value==="undefined"){
				return "";
			}
			return value;
		}else{
			return "";
		}
    }
    if(arguments[0]===""){
    	cacher.removeData(mapKey_user_logo);
        return;
    }
    cacher.saveData(mapKey_user_logo,""+arguments[0]);
};
module.exports.userNick = function(){
	if(arguments.length==0){
		if(cacher.hasData(mapKey_user_nick)){
			var value = cacher.loadData(mapKey_user_nick);
			if(!value || value==null || value==="undefined"){
				return "";
			}
			return value;
		}else{
			return "";
		}
    }
    if(arguments[0]===""){
    	cacher.removeData(mapKey_user_nick);
        return;
    }
    cacher.saveData(mapKey_user_nick,""+arguments[0]);
};
/** 是否已身份认证:1-是;其它-否 **/
module.exports.hasIdentityAuth = function(){
	if(arguments.length==0){
		if(cacher.hasData(mapKey_hasIdentityAuth)){
			var value = cacher.loadData(mapKey_hasIdentityAuth);
			if(!value || value==null || value==="undefined"){
				return "";
			}
			return value;
		}else{
			return "";
		}
    }
    if(arguments[0]===""){
    	cacher.removeData(mapKey_hasIdentityAuth);
        return;
    }
    cacher.saveData(mapKey_hasIdentityAuth,""+arguments[0]);
};
/** 是否社区住户认证:1-是;其它-否 **/
module.exports.hasCommunityAuth = function(){
	if(arguments.length==0){
		if(cacher.hasData(mapKey_hasCommunityAuth)){
			var value = cacher.loadData(mapKey_hasCommunityAuth);
			if(!value || value==null || value==="undefined"){
				return "";
			}
			return value;
		}else{
			return "";
		}
    }
    if(arguments[0]===""){
    	cacher.removeData(mapKey_hasCommunityAuth);
        return;
    }
    cacher.saveData(mapKey_hasCommunityAuth,""+arguments[0]);
};
module.exports.userLoginTokenKey = function(){
	return mapKey_user_login_token;
};
module.exports.userLogoKey = function(){
	return mapKey_user_logo;
};
module.exports.userNickKey = function(){
	return mapKey_user_nick;
};
module.exports.authKey = function(){
	return auth_key;
};
module.exports.authValue = function(){
	return auth_value;
};
module.exports.defaultLogo = function(){
	return "source://image/common/userLogin.png";
};